package com.ronds.webtest.steps;

import com.ronds.webtest.constant.Constant;
import com.ronds.webtest.exception.CommonException;
import org.apache.log4j.Logger;
import org.testng.annotations.Test;
import pages.BasePage;
import pages.LoginPage;

import static com.ronds.webtest.util.PageManager.getPage;
import static com.ronds.webtest.util.WebElementUtil.*;

/**
 * Created by zhanghui on 2017/2/7.
 */

public class GeneralStepDef {

    private static final Logger logger = Logger.getLogger(GeneralStepDef.class);
    LoginPage loginPage = getPage(Constant.LOGIN_URL, LoginPage.class);

    //登录
    public void login(String employeeName, String username, String password) {
        {
            BasePage basePage = new BasePage();
            refresh();
            if (isExist(basePage.getXpathEmployeeNameIcon())) {
                basePage.clickEmployeeIconAndExit();
                isNotVisual(basePage.getXpathEmployeeNameIcon());
                try {
                    isVisual(loginPage.getXpathUsername());
                } catch (Exception e) {
                    takeErrorScreenshotAll();
                    throw new CommonException("密码输入框不可见");
                }
            }
            if (isExist(basePage.getXpathLoadingTag())) {
                isNotVisual(basePage.getXpathLoadingTag());
            }
            if (isExist(basePage.getXpathLoginLoadingTag())) {
                isNotVisual(basePage.getXpathLoginLoadingTag());
            }
            logger.info("用户" + employeeName + "正在登录每刻");
            loginPage.login(username, password);
            if (isExist(basePage.getXpathServiceErrorTag())) {
                throw new CommonException("登录时服务器出错");
            }
            //等待至用户头像出现
            try {
                isVisual(basePage.getXpathEmployeeNameIcon());
            } catch (Exception e) {
                e.getMessage();
                throw new CommonException(employeeName + "登录失败");
            }
            logger.info(username + "登录成功");
        }

    }
}


